home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 4.2 - Palette / Palette.c next >
C/C++ Source or Header  |  1991-08-21  |  5KB  |  258 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    Palette Code from Chapter Four of                        */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #include "Palettes.h"
  15. #include "Picker.h"
  16.  
  17. #define BASE_RES_ID            400
  18. #define NIL_POINTER            0L
  19. #define NIL_STRING            "\p"
  20. #define VISIBLE                TRUE
  21. #define HAS_GOAWAY            TRUE
  22. #define MOVE_TO_FRONT        (WindowPtr)-1L
  23. #define REMOVE_ALL_EVENTS    0
  24. #define MIN_SLEEP            0L
  25. #define    NIL_MOUSE_REGION    0L
  26.  
  27. #define PRECISE_TOLERANCE    0x0000
  28. #define NUM_SQUARES            150
  29.  
  30.  
  31. Boolean         IsColor();
  32. WindowPtr        CreateColorWindow();
  33. PaletteHandle    MakeRedPalette(), MakeBrightPalette(), MakeGrayPalette();
  34.  
  35.  
  36. main()
  37. {
  38.     Point            corner;
  39.     WindowPtr        window;
  40.     PaletteHandle    pal;
  41.     
  42.     ToolBoxInit();
  43.     
  44.     if ( ! IsColor() )
  45.         DoAlert( "\pThis machine does not support Color QuickDraw!" );
  46.     else
  47.     {
  48.         corner.h = 10;
  49.         corner.v = 40;
  50.         window = CreateColorWindow( corner, "\pRed Palette" );
  51.         pal = MakeRedPalette();
  52.         SetPalette( window, pal, TRUE );
  53.         
  54.         corner.h = 170;
  55.         corner.v = 177;
  56.         window = CreateColorWindow( corner, "\pBright Palette" );
  57.         pal = MakeBrightPalette();
  58.         SetPalette( window, pal, TRUE );
  59.         
  60.         corner.h = 330;
  61.         corner.v = 40;
  62.         window = CreateColorWindow( corner, "\pGray Palette" );
  63.         pal = MakeGrayPalette();
  64.         SetPalette( window, pal, TRUE );
  65.         
  66.         DoEventLoop();
  67.     }
  68. }
  69.  
  70.  
  71. /*********************************** ToolBoxInit */
  72.  
  73. ToolBoxInit()
  74. {
  75.     InitGraf( &thePort );
  76.     InitFonts();
  77.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs( NIL_POINTER );
  82.     InitCursor();
  83. }
  84.  
  85.  
  86. /*********************************** DoEventLoop */
  87.  
  88. DoEventLoop()
  89. {
  90.     Boolean            done;
  91.     EventRecord        e;
  92.     short            part;
  93.     WindowPtr        window;
  94.     
  95.     done = FALSE;
  96.     while ( ! done )
  97.     {
  98.         WaitNextEvent( everyEvent, &e, MIN_SLEEP, NIL_MOUSE_REGION );
  99.         
  100.         switch( e.what )
  101.         {
  102.             case mouseDown:
  103.                 part = FindWindow( e.where, &window );
  104.                 if ( part == inGoAway )
  105.                     done = TRUE;
  106.                 else if ( part == inDrag )
  107.                     DragWindow( window, e.where, &screenBits.bounds );
  108.                 else if ( part == inContent )
  109.                 {
  110.                     if ( window != FrontWindow() )
  111.                         SelectWindow( window );
  112.                 }
  113.                 break;
  114.             case updateEvt:
  115.                 BeginUpdate( (WindowPtr)e.message );
  116.                 SetPort( (WindowPtr)e.message );
  117.                 DrawBullseye();
  118.                 EndUpdate( (WindowPtr)e.message );
  119.                 break;
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. /*********************************** DrawBullseye */
  126.  
  127. DrawBullseye()
  128. {
  129.     int        i, center;
  130.     Rect    r;
  131.     
  132.     center = NUM_SQUARES;
  133.     
  134.     for ( i=1; i<=NUM_SQUARES; i++ )
  135.     {
  136.         PmForeColor( i - 1 );
  137.         r.top = center - i;
  138.         r.left = center - i;
  139.         r.bottom = center + i;
  140.         r.right = center + i;
  141.         
  142.         FrameRect( &r );
  143.     }
  144. }
  145.  
  146.  
  147. /******************************** IsColor *********/
  148.  
  149. Boolean IsColor()
  150. {
  151.     SysEnvRec    mySE;
  152.     
  153.     SysEnvirons( 1, &mySE );
  154.     return( mySE.hasColorQD );
  155. }
  156.  
  157.  
  158. /******************************** MakeRedPalette *********/
  159.  
  160. PaletteHandle    MakeRedPalette()
  161. {
  162.     RGBColor        c;
  163.     long            i;
  164.     PaletteHandle    redPalette;
  165.     
  166.     redPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
  167.                             pmTolerant, PRECISE_TOLERANCE );
  168.     
  169.     c.green = 0;
  170.     c.blue = 0;
  171.     
  172.     for ( i=0; i<NUM_SQUARES; i++ )
  173.     {
  174.         c.red = (i * 65535) / NUM_SQUARES;
  175.         SetEntryColor( redPalette, i, &c );
  176.     }
  177.     
  178.     return( redPalette );
  179. }
  180.  
  181.  
  182. /******************************** MakeBrightPalette *********/
  183.  
  184. PaletteHandle    MakeBrightPalette()
  185. {
  186.     PaletteHandle    brightPalette;
  187.     long            i;
  188.     RGBColor        rgbColor;
  189.     HSVColor        hsvColor;
  190.     
  191.     brightPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
  192.                             pmTolerant, PRECISE_TOLERANCE );
  193.     
  194.     hsvColor.value = 65535;
  195.     hsvColor.saturation = 65535;
  196.     
  197.     for ( i=0; i<NUM_SQUARES; i++ )
  198.     {
  199.         hsvColor.hue = (i * 65535) / NUM_SQUARES;
  200.         HSV2RGB( &hsvColor, &rgbColor );
  201.         SetEntryColor( brightPalette, i, &rgbColor );
  202.     }
  203.     
  204.     return( brightPalette );
  205. }
  206.  
  207.  
  208. /******************************** MakeGrayPalette *********/
  209.  
  210. PaletteHandle    MakeGrayPalette()
  211. {
  212.     PaletteHandle    grayPalette;
  213.     long            i;
  214.     RGBColor        rgbColor;
  215.     
  216.     grayPalette = NewPalette( NUM_SQUARES, NIL_POINTER,
  217.                             pmTolerant, PRECISE_TOLERANCE );
  218.     
  219.     for ( i=0; i<NUM_SQUARES; i++ )
  220.     {
  221.         rgbColor.red = (i * 65535) / NUM_SQUARES;
  222.         rgbColor.green = rgbColor.red;
  223.         rgbColor.blue = rgbColor.red;
  224.         SetEntryColor( grayPalette, i, &rgbColor );
  225.     }
  226.     
  227.     return( grayPalette );
  228. }
  229.  
  230.  
  231. /*********************************** CreateColorWindow */
  232.  
  233. WindowPtr    CreateColorWindow( corner, title )
  234. Point    corner;
  235. Str255    title;
  236. {
  237.     WindowPtr    cWindow;
  238.     Rect        r;
  239.     
  240.     SetRect( &r, corner.h, corner.v, corner.h + (2 * NUM_SQUARES),
  241.                         corner.v + (2 * NUM_SQUARES) );
  242.                         
  243.     cWindow = NewCWindow( NIL_POINTER, &r, title,
  244.             VISIBLE, noGrowDocProc, MOVE_TO_FRONT,
  245.             HAS_GOAWAY, NIL_POINTER );
  246.     
  247.     return( cWindow );
  248. }
  249.  
  250.  
  251. /*********************************** DoAlert */
  252.  
  253. DoAlert( s )
  254. Str255        s;
  255. {
  256.     ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
  257.     NoteAlert( BASE_RES_ID, NIL_POINTER );
  258. }